package com.che.software.testato.util.jsf.faces;

import java.io.Serializable;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.che.software.testato.domain.enumeration.AssignmentStatus;
import com.che.software.testato.domain.enumeration.CriterionTypes;
import com.che.software.testato.domain.enumeration.Operator;
import com.che.software.testato.domain.enumeration.SelectionTypes;
import com.che.software.testato.domain.enumeration.SelectiveArea;

/**
 * Static class used to acces to bundle resources inside other classes.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @see Serializable.
 * @since July, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
public class LocaleUtil implements Serializable {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(LocaleUtil.class);
	private static final long serialVersionUID = 1615569748729227880L;
	private static final String ASSIGNED_STATUS_NAME = "assigned", ENDED_STATUS_NAME = "ended", NOT_ASSIGNED_STATUS_NAME = "notAssigned", OPERATOR_AND_NAME = "and", OPERATOR_OR_NAME = "or", RESOURCE_BUNDLE_NAME = "messages", SELECTION_DRASTIC_NAME = "drastic", SELECTION_ELITIST_NAME = "elitist", SELECTION_FLEXIBLE_NAME = "flexible", SELECTION_SAFE_NAME = "safe", SELECTION_TOTAL_NAME = "total", SELECTION_DRASTIC_EXPLANATIONS_NAME = "drasticExplanations", SELECTION_ELITIST_EXPLANATIONS_NAME = "elitistExplanations", SELECTION_FLEXIBLE_EXPLANATIONS_NAME = "flexibleExplanations", SELECTION_SAFE_EXPLANATIONS_NAME = "safeExplanations", SELECTION_TOTAL_EXPLANATIONS_NAME = "totalExplanations";
	public static final String AREA_HIGH_NAME = "high", AREA_LOW_NAME = "low", AREA_MEDIUM_NAME = "medium", COST_CRITERION_NAME = "cost", FIT_CRITERION_NAME = "fit", RISK_CRITERION_NAME = "risk", VALUE_CRITERION_NAME = "value";

	/**
	 * Getter for the string related to an AssignmentStatus enumeration.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param status the given status.
	 * @return the related string, localized in the resource bundle.
	 * @since July, 2011.
	 */
	public static String getAssignmentStatusTranslation(AssignmentStatus status) {
		LOGGER.debug("getAssignmentStatusTranslation(" + status.name() + ").");
		switch (status) {
		case ASSIGNED:
			return getResourceBundleStringByName(ASSIGNED_STATUS_NAME);
		case ENDED:
			return getResourceBundleStringByName(ENDED_STATUS_NAME);
		case NOT_ASSIGNED:
			return getResourceBundleStringByName(NOT_ASSIGNED_STATUS_NAME);
		}
		return "";
	}

	/**
	 * Getter for the string related to an CriterionTypes enumeration.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param criterion the given criterion.
	 * @return the related string, localized in the resource bundle.
	 * @since July, 2011.
	 */
	public static String getCriterionTypeTranslation(CriterionTypes criterion) {
		LOGGER.debug("getCriterionTypeTranslation(" + criterion.name() + ").");
		switch (criterion) {
		case COST:
			return getResourceBundleStringByName(COST_CRITERION_NAME);
		case FIT:
			return getResourceBundleStringByName(FIT_CRITERION_NAME);
		case RISK:
			return getResourceBundleStringByName(RISK_CRITERION_NAME);
		case VALUE:
			return getResourceBundleStringByName(VALUE_CRITERION_NAME);
		}
		return "";
	}

	/**
	 * Getter for the string related to an operator enumeration.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param operator the given operator.
	 * @return the related string, localized in the resource bundle.
	 * @since August, 2011.
	 */
	public static String getOperatorTranslation(Operator operator) {
		LOGGER.debug("getOperatorTranslation(" + operator.name() + ").");
		switch (operator) {
		case AND:
			return getResourceBundleStringByName(OPERATOR_AND_NAME);
		case OR:
			return getResourceBundleStringByName(OPERATOR_OR_NAME);
		}
		return "";
	}

	/**
	 * Getter for a string localized in the resource bundle.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param resourceName the name of the resource to recover.
	 * @return the resulting string.
	 * @since July, 2011.
	 */
	public static String getResourceBundleStringByName(String resourceName) {
		LOGGER.debug("getResourceBundleStringByName(" + resourceName + ").");
		return ResourceBundle.getBundle(RESOURCE_BUNDLE_NAME, FacesContext.getCurrentInstance().getViewRoot().getLocale()).getString(resourceName);
	}

	/**
	 * Getter for the string related to a selection type enumeration.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param selection the given selection type.
	 * @return the related string, localized in the resource bundle.
	 * @since August, 2011.
	 */
	public static String getSelectionTranslation(SelectionTypes selection) {
		LOGGER.debug("getSelectionTranslation(" + selection.name() + ").");
		switch (selection) {
		case DRASTIC:
			return getResourceBundleStringByName(SELECTION_DRASTIC_NAME);
		case ELITIST:
			return getResourceBundleStringByName(SELECTION_ELITIST_NAME);
		case FLEXIBLE:
			return getResourceBundleStringByName(SELECTION_FLEXIBLE_NAME);
		case SAFE:
			return getResourceBundleStringByName(SELECTION_SAFE_NAME);
		case TOTAL:
			return getResourceBundleStringByName(SELECTION_TOTAL_NAME);
		}
		return "";
	}

	/**
	 * Getter for the string related to a selection type explanation.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param selection the given selection type.
	 * @return the related string, localized in the resource bundle.
	 * @since August, 2011.
	 */
	public static String getSelectionExplanationTranslation(SelectionTypes selection) {
		LOGGER.debug("getSelectionExplanationTranslation(" + selection.name() + ").");
		switch (selection) {
		case DRASTIC:
			return getResourceBundleStringByName(SELECTION_DRASTIC_EXPLANATIONS_NAME);
		case ELITIST:
			return getResourceBundleStringByName(SELECTION_ELITIST_EXPLANATIONS_NAME);
		case FLEXIBLE:
			return getResourceBundleStringByName(SELECTION_FLEXIBLE_EXPLANATIONS_NAME);
		case SAFE:
			return getResourceBundleStringByName(SELECTION_SAFE_EXPLANATIONS_NAME);
		case TOTAL:
			return getResourceBundleStringByName(SELECTION_TOTAL_EXPLANATIONS_NAME);
		}
		return "";
	}

	/**
	 * Getter for the string related to a selective area enumeration.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param area the given selective area.
	 * @return the related string, localized in the resource bundle.
	 * @since August, 2011.
	 */
	public static String getSelectiveAreaTranslation(SelectiveArea area) {
		LOGGER.debug("getSelectiveAreaTranslation(" + area + ").");
		switch (area) {
		case HIGH:
			return getResourceBundleStringByName(AREA_HIGH_NAME);
		case LOW:
			return getResourceBundleStringByName(AREA_LOW_NAME);
		case MEDIUM:
			return getResourceBundleStringByName(AREA_MEDIUM_NAME);
		}
		return "";
	}
}